package com.joymart.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserRestAuthorityService {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<String> findByUserId(String userId) {
        List<String> authorityNames = namedParameterJdbcTemplate
                .queryForList("select authority_name from user_rest_authority where user_id = :userId" ,
                       Map.of("userId", userId), String.class);
        return authorityNames;
    }

    public void deleteByUserId(String userId) {
        namedParameterJdbcTemplate.update("delete from user_rest_authority where user_id = :userId",
                new MapSqlParameterSource("userId", userId) );
    }

    public void insert(String userId, List<String> authorityNames) {
        for (String authorityName : authorityNames) {
            namedParameterJdbcTemplate.update(" insert into user_rest_authority (user_id, authority_name) " +
                    " values(:userId, :authorityName)", Map.of("userId", userId, "authorityName", authorityName));
        }
    }


    public void change(String userId) {

    }
}
